@extends('admin.layouts.app')

@section('title', '编辑组织')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h3>编辑组织: {{ $organization->name }}</h3>
    <div>
        <a href="{{ route('admin.organizations.show', $organization) }}" class="btn btn-info me-2">
            <i class="fas fa-eye me-1"></i>查看详情
        </a>
        <a href="{{ route('admin.organizations.index') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-1"></i>返回列表
        </a>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <form action="{{ route('admin.organizations.update', $organization) }}" method="POST" enctype="multipart/form-data">
            @csrf
            @method('PUT')
            
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="name" class="form-label">组织名称 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control @error('name') is-invalid @enderror" id="name" name="name" value="{{ old('name', $organization->name) }}" required>
                        @error('name')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="code" class="form-label">组织代码</label>
                        <input type="text" class="form-control @error('code') is-invalid @enderror" id="code" name="code" value="{{ old('code', $organization->code) }}">
                        @error('code')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <small class="text-muted">唯一标识符，可用于API调用</small>
                    </div>
                </div>
            </div>
            
            <div class="mb-3">
                <label for="description" class="form-label">组织描述</label>
                <textarea class="form-control @error('description') is-invalid @enderror" id="description" name="description" rows="3">{{ old('description', $organization->description) }}</textarea>
                @error('description')
                    <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
            
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="parent_id" class="form-label">上级组织</label>
                        <select class="form-select @error('parent_id') is-invalid @enderror" id="parent_id" name="parent_id">
                            <option value="">无上级组织</option>
                            @foreach($organizations as $org)
                                @if($org->id != $organization->id)
                                    <option value="{{ $org->id }}" {{ old('parent_id', $organization->parent_id) == $org->id ? 'selected' : '' }}>{{ $org->name }}</option>
                                @endif
                            @endforeach
                        </select>
                        @error('parent_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="logo" class="form-label">组织Logo</label>
                        @if($organization->logo)
                            <div class="mb-2">
                                <img src="{{ asset('storage/' . $organization->logo) }}" alt="{{ $organization->name }} Logo" class="img-thumbnail" style="max-height: 100px;">
                            </div>
                        @endif
                        <input type="file" class="form-control @error('logo') is-invalid @enderror" id="logo" name="logo">
                        @error('logo')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <small class="text-muted">上传新图片将替换现有Logo</small>
                    </div>
                </div>
            </div>
            
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="address" class="form-label">地址</label>
                        <input type="text" class="form-control @error('address') is-invalid @enderror" id="address" name="address" value="{{ old('address', $organization->address) }}">
                        @error('address')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="phone" class="form-label">联系电话</label>
                        <input type="text" class="form-control @error('phone') is-invalid @enderror" id="phone" name="phone" value="{{ old('phone', $organization->phone) }}">
                        @error('phone')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>
            
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="email" class="form-label">电子邮箱</label>
                        <input type="email" class="form-control @error('email') is-invalid @enderror" id="email" name="email" value="{{ old('email', $organization->email) }}">
                        @error('email')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="website" class="form-label">网站</label>
                        <input type="url" class="form-control @error('website') is-invalid @enderror" id="website" name="website" value="{{ old('website', $organization->website) }}">
                        @error('website')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>
            
            <div class="mb-3">
                <div class="form-check">
                    <input class="form-check-input @error('is_active') is-invalid @enderror" type="checkbox" id="is_active" name="is_active" value="1" {{ old('is_active', $organization->is_active) ? 'checked' : '' }}>
                    <label class="form-check-label" for="is_active">组织状态为活跃</label>
                    @error('is_active')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
            </div>
            
            <hr>
            
            <h5>组织成员</h5>
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="admin_users" class="form-label">管理员</label>
                        <select class="form-select @error('admin_users') is-invalid @enderror" id="admin_users" name="admin_users[]" multiple>
                            @foreach(\App\Models\User::all() as $user)
                                <option value="{{ $user->id }}" {{ (old('admin_users') ? in_array($user->id, old('admin_users')) : $organization->admins->contains($user->id)) ? 'selected' : '' }}>{{ $user->name }} ({{ $user->email }})</option>
                            @endforeach
                        </select>
                        @error('admin_users')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <small class="text-muted">按住Ctrl键可多选</small>
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="member_users" class="form-label">普通成员</label>
                        <select class="form-select @error('member_users') is-invalid @enderror" id="member_users" name="member_users[]" multiple>
                            @foreach(\App\Models\User::all() as $user)
                                <option value="{{ $user->id }}" {{ (old('member_users') ? in_array($user->id, old('member_users')) : ($organization->users->contains($user->id) && !$organization->admins->contains($user->id))) ? 'selected' : '' }}>{{ $user->name }} ({{ $user->email }})</option>
                            @endforeach
                        </select>
                        @error('member_users')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <small class="text-muted">按住Ctrl键可多选</small>
                    </div>
                </div>
            </div>
            
            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                <button type="reset" class="btn btn-light me-md-2">重置</button>
                <button type="submit" class="btn btn-primary">更新组织</button>
            </div>
        </form>
    </div>
</div>
@endsection